import Vue from 'vue'
import Vuex from 'vuex'
//导入接口
import { uesrInfoAPI } from "@/api";
Vue.use(Vuex)
import { settoken, getToken, setuserchannels } from '../utils/auth'
//导入轻提示
import { Toast } from 'vant';
//导入路由
import router from '@/router'
const store = new Vuex.Store({
    state: {
        tokenobj: getToken() || {},
        userInfo: {},
    },
    mutations: {
        setTokenobj(state, tokenObj) {
            state.tokenobj = tokenObj
            settoken(tokenObj)
        },
        setuserInfo(state, userInfo) {
            state.userInfo = userInfo
        },
        layout(state) {
            state.tokenobj = {}
            state.userInfo = {}
            Toast.success('退出登陆成功');
            router.push('/login')
        }

    },
    actions: {
        async reqUserInfo(context) {
            if (!context.state.userInfo.name) {
                // 发请求获取用户资料
                let res = await uesrInfoAPI();
                // this.user = res.data.data;
                context.commit("setuserInfo", res.data.data);
            }
        }
    }
})
export default store